package com.manage.dao;

import com.manage.domain.Emp;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface IEmpDao {

    @Select({
            "select e.*, d.name deptName from emp e left join dept d on e.dept_id = d.id",
            "order by e.update_time desc"
    })
    List<Emp> list();

    @Insert("insert into emp(username,password,name,gender,phone,job,salary,image,entry_date,dept_id) " +
            "values(#{username},#{password},#{name},#{gender},#{phone},#{job},#{salary},#{image},#{entryDate},#{deptId})")
    void insert(Emp emp);

    @Update({
            "<script>",
            "UPDATE emp",
            "<set>",
            "<if test=\"username != null and username != ''\">username = #{username},</if>",
            "<if test=\"password != null and password != ''\">password = #{password},</if>",
            "<if test=\"name != null and name != ''\">name = #{name},</if>",
            "<if test=\"gender != null\">gender = #{gender},</if>",
            "<if test=\"phone != null and phone != ''\">phone = #{phone},</if>",
            "<if test=\"job != null\">job = #{job},</if>",
            "<if test=\"salary != null\">salary = #{salary},</if>",
            "<if test=\"image != null and image != ''\">image = #{image},</if>",
            "<if test=\"entryDate != null\">entry_date = #{entryDate},</if>",
            "<if test=\"deptId != null\">dept_id = #{deptId},</if>",
            "<if test=\"updateTime != null\">update_time = #{updateTime},</if>",
            "</set>",
            "WHERE id = #{id}",
            "</script>"
    })
    void update(Emp emp);

    @Delete("delete from emp where id=#{id}")
    void deleteById(Integer id);

    @Select("select count(1) from emp where dept_id=#{deptId}")
    int countByDeptId(Integer deptId);
}


